package com.eightgroups.tces.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.eightgroups.common.result.JsonResult;
import com.eightgroups.common.utils.StringUtil;
import com.eightgroups.sys.entity.Permission;
import com.eightgroups.sys.entity.RolePermission;
import com.eightgroups.sys.entity.ZNodes;
import com.eightgroups.tces.entity.Indicator;
import com.eightgroups.tces.entity.QtypeIndicator;
import com.eightgroups.tces.service.IIndicatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import com.eightgroups.common.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 指标 前端控制器
 * </p>
 *
 * @author zzc2016.cn
 * @since 2017-11-13
 */
@Controller
@RequestMapping("/admin/indicator")
public class IndicatorController extends BaseController {
    @Autowired
    private IIndicatorService iIndicatorService;
    /**
     * 指标列表页面
     * @return
     */
    @GetMapping("/list")
    public String list() {
        return "/admin/indicator/list";
    }

    /**
     * 获取指标列表
     * @return
     */
    @ResponseBody
    @GetMapping("/getList")
    public List<Indicator> getIndicatorList() {
        // 排序
        EntityWrapper<Indicator> wrapper = new EntityWrapper<>();
        wrapper.orderBy("sort", true);
        List<Indicator> indicators = iIndicatorService.selectList(wrapper);
        return indicators;
    }

    /**
     * 添加指标
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addIndicator")
    public JsonResult addIndicator(Indicator indicator) {
        if (indicator.getType() == 0) {
            indicator.setPid(0l);
        }
        return iIndicatorService.insert(indicator) ? renderSuccess("添加成功") : renderError("添加失败");
    }
    /**
     * 修改指标
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateIndicator")
    public JsonResult updateIndicator(Indicator indicator) {
        if (indicator.getType() == 0) {
            indicator.setPid(0l);
        }
        return iIndicatorService.updateById(indicator) ? renderSuccess("修改成功") : renderError("修改失败");
    }
    /**
     * 删除指标
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(@RequestParam(value = "id", required = false) String id) {
        if (StringUtil.isEmpty(id)) {
            return renderError("请选择数据");
        }
        EntityWrapper<Indicator> wrapper = new EntityWrapper<>();
        wrapper.eq("pid", id);
        List<Indicator> indicators = iIndicatorService.selectList(wrapper);
        if (indicators.size() >= 1) {
            return renderError("请删除子节点数据");
        }
        return iIndicatorService.deleteById(Long.valueOf(id)) ? renderSuccess("删除成功") : renderError("删除失败");
    }
    /**
     * 查询所有指标树形展示
     * @param pid
     * @return
     */
    @ResponseBody
    @RequestMapping("/getIndicatorZTreeNodes")
    public List<ZNodes> getIndicatorZTreeNodes(Long pid){
        return iIndicatorService.findIndicatorZTreeNodes(pid);
    }

    /**
     * 获取父级select
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getTypeIndicator/{type}")
    public List<Indicator> getTypePermission(@PathVariable("type") Integer type) {
        if (type == null) {
            type = 0;
        }
        if (type < 0 || type >1) {
            return null;
        }
        EntityWrapper<Indicator> indicatorEntityWrapper = new EntityWrapper<>();
        indicatorEntityWrapper.eq("type", type);
        indicatorEntityWrapper.orderBy("sort", true);
        List<Indicator> indicators = iIndicatorService.selectList(indicatorEntityWrapper);
        return indicators;
    }
    /**
     * 传入Id 查询一条权限值返回
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getIndicatorById")
    public Indicator getIndicatorById(Long id) {
        return iIndicatorService.selectById(id);
    }

    /**
     * 修改问卷指标
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/modifyQtypeIndicator")
    public JsonResult modifyQtypeIndicator(@RequestBody List<QtypeIndicator> qtypeIndicators) {
        return iIndicatorService.modifyQtypeIndicator(qtypeIndicators) ? renderSuccess("分配成功"): renderError("分配失败");
    }
}
